package com.wmw.music;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.os.IBinder;

public class MediaPlayerService extends Service implements OnCompletionListener,OnErrorListener,Runnable{
	private MediaPlayer mp;
	private ExecutorService es;
	private boolean fl = true;
	private Intent intent;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		mp = new MediaPlayer();
		mp.setOnCompletionListener(this);
		intent = new Intent(Constant.ACTION);
		super.onCreate(); 
	} 

	@Override
	public void onStart(Intent intent, int startId) {
		super.onStart(intent, startId);
		es = Executors.newSingleThreadExecutor();
		if(intent != null){
			String flag = intent.getStringExtra(Constant.FLAG);
			int position = intent.getIntExtra(Constant.POSITION, 0); 
			play(intent, position, flag);
		}
	}

	private void play(Intent intent, int position, String flag) {
		try {
			if(flag.equals(Constant.NEXT)){
				next();
			}else if(flag.equals(Constant.PREVIOUS)){
				previous();
			}else if(flag.equals(Constant.ALL)){
				String _data = MusicData.allMusicList.get(position).get_data();
				startPlay(_data);  
			}else if(flag.equals(Constant.ALBUM)){
				String _data = MusicData.albumList.get(Constant.groupid).get(position).get_data();
				startPlay(_data);  
			}else if(flag.equals(Constant.ARTIST)){
				String _data = MusicData.artistList.get(Constant.groupid).get(position).get_data();
				startPlay(_data);  
			}else if(flag.equals(Constant.SEEKBAR)){
				mp.seekTo(position);
			}else if(flag.equals(Constant.PLOPA)){
				switch(Constant.currentState){
				case Constant.PAUSE:
					mp.pause();
					break;
				case Constant.PLAY:
					mp.start();
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private void startPlay(String _data) throws IOException {
		reset(); 
		mp.setDataSource(_data);
		init();
	}

	private void reset() {
		fl = false;
		mp.stop();
		mp.reset();
	}

	private void init() throws IOException {
		mp.prepare();
		mp.start();
		fl = true;
		Constant.currentState = Constant.PLAY;
		es.execute(this);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	public void run() {
		while(fl){
			if((mp.getCurrentPosition() < mp.getDuration())){
				intent.putExtra(Constant.FLAG, Constant.CURR);
				intent.putExtra(Constant.CURR, mp.getCurrentPosition());
				Constant.seekBarCurr = mp.getCurrentPosition();
				sendBroadcast(intent);
			}else{
				fl = false; 
			}
			try {
				Thread.sleep(400);
			} catch (InterruptedException e) {  
				e.printStackTrace();
			}
		}
	}

	public void onCompletion(MediaPlayer mp) {
		switch(Constant.playModler){
		case Constant.loop:
			loop();
			break;
		case Constant.order:
			next();
			break;
		case Constant.random:
			random();
			break;
		}
//		PlayMusic.serchLrc();
		Intent intent = new Intent(Constant.UPDATACTION);
		sendBroadcast(intent);
		sendBRDisplay();
	}

	private void next() {
		if(Constant.currentArr.equals(Constant.ALL)){
			if(Constant.currentIndex + 1 > MusicData.allMusicList.size() - 1){
				Constant.currentIndex = 0;
			}else{
				Constant.currentIndex++;
			}
			setAllMusic();
			play();
		}else if(Constant.currentArr.equals(Constant.ARTIST)){
			if(Constant.currentIndex + 1 > MusicData.artistList.get(Constant.groupid).size() - 1){
				Constant.currentIndex = 0;
			}else{
				Constant.currentIndex++;
			}
			setArtist();
			play();
		}else if(Constant.currentArr.equals(Constant.ALBUM)){
			if(Constant.currentIndex + 1 > MusicData.albumList.get(Constant.groupid).size() - 1){
				Constant.currentIndex = 0;
			}else{
				Constant.currentIndex++;
			}
			setAlbum();
			play();
		}
//		PlayMusic.serchLrc();
		sendBRDisplay();
	}

	private void previous() {
		if(Constant.currentArr.equals(Constant.ALL)){
			if(Constant.currentIndex - 1 < 0){
				Constant.currentIndex = MusicData.allMusicList.size() - 1;
			}else{
				Constant.currentIndex--;
			}
			setAllMusic();
			play();
		}else if(Constant.currentArr.equals(Constant.ARTIST)){
			if(Constant.currentIndex - 1 < 0){
				Constant.currentIndex = MusicData.artistList.get(Constant.groupid).size() - 1;
			}else{
				Constant.currentIndex--;
			}
			setArtist();
			play();
		}else if(Constant.currentArr.equals(Constant.ALBUM)){
			if(Constant.currentIndex - 1 < 0){
				Constant.currentIndex = MusicData.albumList.get(Constant.groupid).size() - 1;
			}else{
				Constant.currentIndex--;
			}
			setAlbum();
			play();
		}
//		PlayMusic.serchLrc();
		sendBRDisplay();
	}
	
	private void sendBRDisplay() {
		intent.putExtra(Constant.FLAG, Constant.UPDATADIAPALY);
		sendBroadcast(intent);
	}

	private void loop(){
		if(Constant.currentArr.equals(Constant.ALL)){
			setAllMusic();
			play();
		}else if(Constant.currentArr.equals(Constant.ARTIST)){
			setArtist();
			play();
		}else if(Constant.currentArr.equals(Constant.ALBUM)){
			setAlbum();
			play();
		}
	}

	private void random() {
		if(Constant.currentArr.equals(Constant.ALL)){
			int size = MusicData.allMusicList.size();
			randomFactory(size);
			setAllMusic();
			play();
		}else if(Constant.currentArr.equals(Constant.ARTIST)){
			int size = MusicData.artistList.get(Constant.groupid).size();
			randomFactory(size);
			setArtist();
			play();
		}else if(Constant.currentArr.equals(Constant.ALBUM)){
			int size = MusicData.albumList.get(Constant.groupid).size();
			randomFactory(size);
			setAlbum();
			play();
		}
	}

	private void randomFactory(int size) {
		Random random = new Random();
		Constant.currentIndex = random.nextInt(size);
	}

	private void setAllMusic() {
		Constant.title = MusicData.allMusicList.get(Constant.currentIndex).getTitle();
		Constant.artist = MusicData.allMusicList.get(Constant.currentIndex).getArtist();
		Constant.total = Util.toTime(MusicData.allMusicList.get(Constant.currentIndex).getDuration());
		Constant.duration = MusicData.allMusicList.get(Constant.currentIndex).getDuration();
		Constant._data = MusicData.allMusicList.get(Constant.currentIndex).get_data();
	}

	private void setAlbum() {
		Constant.title = MusicData.albumList.get(Constant.groupid).get(Constant.currentIndex).getTitle();
		Constant.artist = MusicData.albumList.get(Constant.groupid).get(Constant.currentIndex).getArtist();
		Constant.total = Util.toTime(MusicData.albumList.get(Constant.groupid).get(Constant.currentIndex).getDuration());
		Constant.duration = MusicData.albumList.get(Constant.groupid).get(Constant.currentIndex).getDuration();
		Constant._data = MusicData.albumList.get(Constant.groupid).get(Constant.currentIndex).get_data();
	}

	private void setArtist() {
		Constant.title = MusicData.artistList.get(Constant.groupid).get(Constant.currentIndex).getTitle();
		Constant.artist = MusicData.artistList.get(Constant.groupid).get(Constant.currentIndex).getArtist();
		Constant.total = Util.toTime(MusicData.artistList.get(Constant.groupid).get(Constant.currentIndex).getDuration());
		Constant.duration = MusicData.artistList.get(Constant.groupid).get(Constant.currentIndex).getDuration();
		Constant._data = MusicData.artistList.get(Constant.groupid).get(Constant.currentIndex).get_data();
	}

	private void play(){
		try {
			if(Constant.currentArr.equals(Constant.ALL)){
				reset(); 
				mp.setDataSource(MusicData.allMusicList.get(Constant.currentIndex).get_data());
				init();  
			}else if(Constant.currentArr.equals(Constant.ALBUM)){
				reset();
				mp.setDataSource(MusicData.albumList.get(Constant.groupid).get(Constant.currentIndex).get_data());
				init();
			}else if(Constant.currentArr.equals(Constant.ARTIST)){
				reset();
				mp.setDataSource(MusicData.artistList.get(Constant.groupid).get(Constant.currentIndex).get_data());
				init();
			}
		}catch(IOException e){
			e.printStackTrace();
		}
	}


	public boolean onError(MediaPlayer mp, int what, int extra) {
		mp.reset();
		return false;
	}
}
